windowloads()

var username = getCookie('username')  
var userid = getCookie('userid')  

// 显示当前登录的用户名
if(username){
    $('.login-rge')[0].outerHTML= `
    <li  class="login-rge">
     <a class = "del" onclick = outlogin()>退出</a>
    </li>
     `
}

//用户确定退出时，删除cookie    
function outlogin(){outloginfn()}
$('.user h3')[0].innerText = username
$('.updata').validate({
    rules: {
        tel: {
            number: true,
            minlength: 5,
            maxlength: 11
        },
        password: {
            required: true, 
            minlength: 6, 
            maxlength: 16, 
        },
        newpassword: {
            required: true, 
            minlength: 6, 
            maxlength: 16, 
        },
        reglogCode:{
            required: true, 
        }
    },
    messages: {
        tel: {
            number: '请输入合法的手机号',
            minlength: '最短5位',
            maxlength: '最长11位'
        },
        password: {
            required: '密码为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大16位', 

        },
        newpassword: {
            required: '确认密码不能为空', 
            minlength: '长度最小6位', 
            maxlength: '长度最大16位', 
        },
        reglogCode:{
            required: '验证码不能为空', 
        }

    },
    
// 表单提交事件
submitHandler: function (form) {
        let loadcode =  $('#reglogCode')[0].value 
        var captcha = getCookie('captcha')  
        if(loadcode.toUpperCase() == captcha.toUpperCase()){
        let formData = $('.updata').serialize()
        let loadid = layer.load(1, {
            shade: [0.5, '#000']
        })
        $.ajax({
            method: 'post',
            url: '/users//api/pass/'+userid,
            data: formData,
            headers: {
                'Authorization': localStorage.getItem('token')
            },
            success: res => {
                let {
                    errorCode,
                    message
                } = res
                if (errorCode == 0) {
                    layer.msg(message, {
                        icon: 1,
                        time: 2000
                    },function(){
                        location.href = '/login.html'
                    })
                    
                } else {
                    layer.msg(message, {
                        icon: 2,
                        time: 2000
                    })
                    layer.close(loadid)

                }
            }
        })
        }else{ 
        layer.msg('验证码错误请重新输入！', {
            icon: 2,
            time: 2000
        })
        }
    }
})

// 点击图片刷新验证码
$('.loadcode').on('click',function(){
    this.src =
   '/users/api/reglogCode?' +  Math.random();
})

// 获取头像
getheadmig()